package com.kaka.bus.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;

import com.kaka.bus.AppCenter;

/**
 * Created by OnionMac on 2018/1/11.
 */

public class NetChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        /**
         * 当网络状态改变
         */
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            Parcelable data = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != data) {
                NetworkInfo networkInfo = (NetworkInfo) data;
                NetworkInfo.State state = networkInfo.getState();
                boolean isConnected = state == NetworkInfo.State.CONNECTED;
                AppCenter.onNetChange(isConnected);
            }
        }
    }
}
